package config
{
    import com.adobe.serialization.json.JSON;
    
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.filesystem.File;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;
    import flash.filters.ColorMatrixFilter;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.net.ObjectEncoding;
    import flash.net.URLLoader;
    import flash.net.URLRequest;

    [Bindable]
    public class RekooConfig
    {
        private static var _instance:RekooConfig;

        //版本控制
        private var _cfg:Object = { "version":"0", "username":"", "cookie":"" };
        private var _newversion:String = "";
        public var initialized:Boolean = false;
        private var connectSuccess:Boolean = false;
        private var loader:URLLoader;
        private var file:File;
        public var updateList:Array = [];
        public var Debug:Boolean = false;
        
        //图书馆中热度的图片数据
        [Embed(source="assets/skin/buttonAssets.swf",symbol="Star")]
        private var starImage:Class;
		private var _starData:BitmapData = null;
		private var _starDataRGB:BitmapData = null;
		
		//整个系统的后端地址
        public static const ServerURL:String = "http://192.168.1.90/rekoo/";
		
		public static var AppFolder:String ="";
        public function RekooConfig(enforcer:SingletonEnforcer)
        {
            if(enforcer == null)
            {
                throw new Error("Singleton Error: You Can Only Have One ConfigReader Instance");
            }
            else
            {
            	RekooConfig.AppFolder = File.applicationDirectory.nativePath;
                file = new File(RekooConfig.AppFolder+"/config/rekoo.config");
                readConfig();
                loadVersion();
            }
        }

        private function loadVersion():void
        {
            loader = new URLLoader();
            var request:URLRequest = new URLRequest(RekooConfig.ServerURL + "version.php");
            loader.addEventListener(Event.COMPLETE,onResponse);
            loader.addEventListener(IOErrorEvent.IO_ERROR,onError);
            loader.load(request);
        }
        
        private function onResponse(e:Event):void
        {
        	if(e.target.hasOwnProperty("data"))
        	{
        		var serverCfg:Object = JSON.decode(e.target.data);
            	_newversion = serverCfg.version;
            	updateList = serverCfg.files;
            	connectSuccess = true;
            	dispose();
        	}
        	else
        	{
        		dispose();
        	}
        }
        
 		private function onError(e:IOErrorEvent):void
        {
        	dispose();
        }
        
        private function dispose():void
        {
        	loader.removeEventListener(IOErrorEvent.IO_ERROR,onError);
        	loader.removeEventListener(Event.COMPLETE,onResponse);
            loader = null;
            initialized = true;
        }

        public function hasNewVersion():Boolean
        {
			return connectSuccess && _cfg.version < _newversion;
        }

        private function readConfig():void
        {
            if(file.exists)
            {
                var fs:FileStream = new FileStream();
                fs.open(file, FileMode.READ);
                fs.objectEncoding = ObjectEncoding.AMF3;
                _cfg = fs.readObject();
                fs.close();
            }
        }

        public function writeConfig():void
        {
        	_cfg.version=_newversion;
            var fs:FileStream = new FileStream();
            fs.open(file, FileMode.WRITE);
            fs.objectEncoding = ObjectEncoding.AMF3;
            fs.writeObject(_cfg);
            fs.close();
        }

        public static function getInstance():RekooConfig
        {
            if(_instance == null)
            {
                _instance = new RekooConfig(new SingletonEnforcer());
            }
            return _instance;
        }

        public function get version():String
        {
            return _cfg.version;
        }
        
        private var matrix:Array = 
		[
			0.3086, 0.6094, 0.0820, 0, 0,
			0.3086, 0.6094, 0.0820, 0, 0,
			0.3086, 0.6094, 0.0820, 0, 0, 
			0, 0, 0, 1, 0
		];
        
        public function getStarData(value:int):BitmapData
        {
        	if(_starData == null){
        		var starSprite:Sprite = new starImage();
        		starSprite.cacheAsBitmap = true;
        		_starDataRGB = new BitmapData(starSprite.width + 5,starSprite.height + 5,true,0x00FFFFFF);
        		_starDataRGB.draw(starSprite);
        		starSprite.filters = [new ColorMatrixFilter(matrix)];
        		_starData = new BitmapData(starSprite.width + 5,starSprite.height + 5,true,0x00FFFFFF);
        		_starData.draw(starSprite);
        	}
        	value = value < 0 ? 0 : value>10 ? 10 : value;
        	var d:int = _starData.width/10;
        	var rgbColor:BitmapData = _starData.clone();
        	rgbColor.copyPixels(_starDataRGB,new Rectangle(0,0,d*value,_starData.height),new Point(0,0));
        	//rgbColor.
        	return rgbColor;
        }


    }
}

class SingletonEnforcer
{
}